package com.jmectf.game.handler;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jmectf.game.actions.DriftAction;
import com.jmectf.game.actions.ForwardAndBackwardAction;
import com.jmectf.game.actions.VehicleRotateAction;
import com.jmectf.game.vehicle.Vehicle;

/**
 * <code>jmectfHandler</code> handles mouse and key inputs. It also manages the
 * vehicle that the user controls.
 * @author Nilay Kumar
 * @version April 19, 2009 8:05 P.M.
 */
public class BattleHandler extends InputHandler {
	
    private Vehicle vehicle;
    private DriftAction drift;
    
    /**
     * <code>update</code> updates the input handler every frame.
     * @param interpolation the time between frames
     * @see InputHandler#update(float)
     */
    public void update(float interpolation) {
        if ( !isEnabled() ) return;
        super.update(interpolation);
        drift.performAction(event);
        vehicle.update(interpolation);
    }
    
    /**
     * Constructor sets up the handler.
     * @param vehicle the node we wish to move.
     * @param api the library that will handle creation of the input.
     */
    public BattleHandler(Vehicle vehicle, String api) {
        this.vehicle = vehicle;
        setKeyBindings(api);
        setActions(vehicle);
    }

    /**
     * <code>setKeyBindings</code> creates the keyboard object, allowing us to obtain the values of a 
     * keyboard as keys are pressed. It then sets the actions to be triggered based on if 
     * certain keys are pressed (WSAD).
     * @param api the library that will handle creation of the input.
     */
    private void setKeyBindings(String api) {
        KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
        keyboard.set("forward", KeyInput.KEY_W);
        keyboard.set("backward", KeyInput.KEY_S);
        keyboard.set("turnRight", KeyInput.KEY_D);
        keyboard.set("turnLeft", KeyInput.KEY_A);
    }

    /**
     * <code>setActions</code> assigns action classes to triggers. These actions handle moving the node forward,
     * backward and rotating it. It also creates an action for drifting that is not assigned to key trigger, 
     * this action will occur each frame.
     * @param node the node to control.
     */
    private void setActions(Vehicle node) {
        ForwardAndBackwardAction forward = new ForwardAndBackwardAction(node, ForwardAndBackwardAction.FORWARD);
        addAction(forward, "forward", true);
        ForwardAndBackwardAction backward = new ForwardAndBackwardAction(node, ForwardAndBackwardAction.BACKWARD);
        addAction(backward, "backward", true);
        VehicleRotateAction rotateLeft = new VehicleRotateAction(node, VehicleRotateAction.LEFT);
        addAction(rotateLeft, "turnLeft", true);
        VehicleRotateAction rotateRight = new VehicleRotateAction(node, VehicleRotateAction.RIGHT);
        addAction(rotateRight, "turnRight", true);
        drift = new DriftAction(node);
    }
}